package MapReduce.Demo13_ProductFilter;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import utils.JobSubmit;

import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/23 10:25
 */
public class ProductFilterApp {

    /**
     * 第一个 Mapper 过滤掉所有商品名大于等于3个字的商品，把符合条件的数据送到下一个步骤
     */
    public static class PFMapper_First extends Mapper<LongWritable, Text,Text, IntWritable>{

        Text outKey = new Text();
        IntWritable outValue = new IntWritable();

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            String line = value.toString();
            String[] fields = line.split("\t");

            String productName = fields[0];
            int price = Integer.parseInt(fields[1]);


            if(productName.length()<3){
                System.out.println("fistMapper:"+productName+",price:"+price);
                outKey.set(productName);
                outValue.set(price);
                context.write(outKey,outValue);
            }

        }
    }

    /**
     * 第二个 Mapper 过滤掉名字中带 “子” 的商品，把符合条件的数据送到下一个步骤
     */
    public static class PFMapper_Second extends Mapper<Text, IntWritable,Text,IntWritable>{
        @Override
        protected void map(Text key, IntWritable value, Mapper<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            String productName = key.toString();

            //把符合条件的数据送出去，但不改变数据的内容
            if(!productName.contains("子")){
                System.out.println("secondMapper:"+productName+",price:"+value.get());
                context.write(key,value);
            }
        }
    }

    /**
     * 第三个 Mapper 过滤掉单价大于10000的商品，把符合条件的数据送到下一个步骤
     */
    public static class PFMapper_Third extends Mapper<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void map(Text key, IntWritable value, Mapper<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            int price  = value.get();

            //把符合条件的数据送出去，但不改变数据的内容
            if(price < 5000){
                System.out.println("thirdMapper:"+key.toString()+",price:"+price);
                context.write(key, value);
            }

        }
    }

    public static class PFReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

        IntWritable outValue = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            int sum = 0;

            System.out.println("reduceKey:"+key.toString());

            for (IntWritable value : values) {
                sum += value.get();
            }

            outValue.set(sum);

            context.write(key,outValue);

        }
    }

    public static void main(String[] args) throws Exception {


        Job job = Job.getInstance();

        ChainMapper.addMapper(
                job,
                PFMapper_First.class,
                LongWritable.class,
                Text.class,
                Text.class,
                IntWritable.class,
                job.getConfiguration()
        );

        ChainMapper.addMapper(
                job,
                PFMapper_Second.class,
                Text.class,
                IntWritable.class,
                Text.class,
                IntWritable.class,
                job.getConfiguration()
        );

        ChainMapper.addMapper(
                job,
                PFMapper_Third.class,
                Text.class,
                IntWritable.class,
                Text.class,
                IntWritable.class,
                job.getConfiguration()
        );

        ChainReducer.setReducer(
                job,
                PFReducer.class,
                Text.class,
                IntWritable.class,
                Text.class,
                IntWritable.class,
                job.getConfiguration()
        );

        Path outputPath = new Path(args[1]);
        FileSystem fs = FileSystem.get(job.getConfiguration());

        if (fs.exists(outputPath)) {
            fs.delete(outputPath,true);
        }

        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,outputPath);

        job.waitForCompletion(true);

    }



}
